package socket;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.sun.security.ntlm.Client;

import Static.StaticTest;

public class MyServer {
	public static void main(String[] args) throws Exception {
		ServerSocket server = new ServerSocket(5679);
		MyServer myServer = new MyServer();
		Socket socket = null;
		Thread operateThread = new Thread() {
			public void run() {
				while (true) {
					HashMap<String, Socket> map = StaticTest.socketMap;
					HashMap<String, Thread> threadMap = StaticTest.threadMap;
					System.out.println(map.size());
					if (map.size() > 0) {
						System.out.println("op2");
						Iterator<Map.Entry<String, Socket>> iterator = map.entrySet().iterator();
						Socket socket = iterator.next().getValue();
						try {
							int key = 0;
							while(key<7){
							/*PrintWriter out = new PrintWriter(socket.getOutputStream());
							out.println("yes ok:"+key++);
							System.out.println("op3");
							out.flush();*/
							sleep(1000);
							}
							socket.close();
							iterator.remove();
						} catch (IOException e) {
							e.printStackTrace();
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}
			}
		};
	//	operateThread.start();
		int flag = 0;
		
		while (true) {
			socket = server.accept();
			StaticTest.socketMapPut(socket.getInetAddress().getHostAddress() + flag++, socket);
			System.out.println(StaticTest.socketMap.size());
			//PrintWriter out = new PrintWriter(socket.getOutputStream());
//			int k = 0;
//			while(k<10){
//				out.println("hehhehhe"+flag++);
//			}
			//out.flush();
			Thread thread = myServer.new mkServer(socket);
			thread.setName(socket.getInetAddress().getHostAddress());
			StaticTest.threadMapPut(socket.getInetAddress().getHostAddress(), thread);
			thread.start();
		}
	}

	public class mkServer extends Thread {
		Socket sSocket = new Socket();

		public mkServer(Socket sSocket) {
			this.sSocket = sSocket;
		}

		public void run() {
			try {
				sockets(sSocket);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public static void mkFile(BufferedReader in) throws IOException {
		System.out.println("hh");
		java.io.File file = new java.io.File("D://a.txt");
		FileOutputStream fos = new FileOutputStream(file);
		try {
			fos.write(in.readLine().getBytes(), 0, 3);
		} catch (Exception e) {
			e.printStackTrace();
		}
		fos.close();
	}

	public static void sockets(Socket client) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
		PrintWriter out = new PrintWriter(client.getOutputStream());
		int i = 0;
		while (true) {
			String str = in.readLine();
			System.out.println("server:" + str);
			out.println("has received:" + str);
			out.flush();
			/*try {
				mkFile(in);
			} catch (Exception e) {
				e.printStackTrace();
			}*/
			System.out.println("here");
			if (str.equals("end"))
				break;
		}
		client.close();
	}
}
